﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.REST.Utility;
using System.Collections;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// MenuSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MenuItemSummaryWindow : UserControl
    {
        private List<RS_MenuItemLabel> _labels = null;
        private List<RS_Unit> _units = null;
        private List<RS_MenuItemType> _menuTypes = null;

        public MenuItemSummaryWindow()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            _labels = Aide.LocalClient.Invoke<List<RS_MenuItemLabel>>(
                service => service.GetAllMenuItemLabels());

            _units = Aide.LocalClient.Invoke<List<RS_Unit>>(
                service => service.GetAllUnits());

            _menuTypes = Aide.LocalClient.Invoke<List<RS_MenuItemType>>(
                service => service.GetAllMenuItemTypes(true));

            ClientUtil.LoadItemsSource(dtgLabel, _labels);
            ClientUtil.LoadItemsSource(dtgType, _menuTypes);
        }

        private void LoadSelectedItem(DataGrid dataGrid, object selectedItem)
        {

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void btnAddLabel_Click(object sender, RoutedEventArgs e)
        {
            AddMenuLabelWindow addMenuLabelWindow = new AddMenuLabelWindow();
            addMenuLabelWindow.InitializeWindow(CrudType.Create);
            addMenuLabelWindow.AddSuccess += new EventHandler(AddMenuLabelWindow_AddSuccess);

            addMenuLabelWindow.ShowDialog();
        }

        private void AddMenuLabelWindow_AddSuccess(object sender, EventArgs e)
        {
            AddMenuLabelWindow addMenuLabelWindow = sender as AddMenuLabelWindow;
            RS_MenuItemLabel newLabel = addMenuLabelWindow.MenuItemLabel;
            _labels.Add(newLabel);

            ClientUtil.LoadSelectedItem(dtgLabel, newLabel);
            addMenuLabelWindow.ResetWindow();
        }

        private void btnModifyLabel_Click(object sender, RoutedEventArgs e)
        {
            RS_MenuItemLabel selectedLabel = dtgLabel.SelectedItem as RS_MenuItemLabel;

            if (selectedLabel != null)
            {
                AddMenuLabelWindow modifyMenuLabelWindow = new AddMenuLabelWindow();
                modifyMenuLabelWindow.MenuItemLabel = selectedLabel;
                modifyMenuLabelWindow.InitializeWindow(CrudType.Update);
                modifyMenuLabelWindow.AddSuccess += new EventHandler(modifyMenuLabelWindow_AddSuccess);

                modifyMenuLabelWindow.ShowDialog();
            }
        }

        void modifyMenuLabelWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyMenuLabelWindow = sender as AddMenuLabelWindow;
            RS_MenuItemLabel newLabel = modifyMenuLabelWindow.MenuItemLabel;

            _labels.Find(delegate(RS_MenuItemLabel label)
            {
                if (label.LabelId == newLabel.LabelId)
                {
                    label = newLabel;
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgLabel, newLabel);
        }

        private void btnDeleteLabel_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnAddMenu_Click(object sender, RoutedEventArgs e)
        {
            RS_MenuItemType selectedType = dtgType.SelectedItem as RS_MenuItemType;

            if (selectedType != null)
            {
                AddMenuItemWindow addMenuItemWindow = new AddMenuItemWindow(selectedType, _units);
                addMenuItemWindow.InitializeWindow(CrudType.Create);
                addMenuItemWindow.AddSuccess += new EventHandler(AddMenuItemWindow_AddSuccess);

                addMenuItemWindow.ShowDialog();
            }
        }

        private void AddMenuItemWindow_AddSuccess(object sender, EventArgs e)
        {
            AddMenuItemWindow addMenuItemWindow = sender as AddMenuItemWindow;
            RS_MenuItem newMenuItem = addMenuItemWindow.MenuItem;

            _menuTypes.Find(delegate(RS_MenuItemType menuType)
            {
                if (menuType.TypeId == newMenuItem.Metadata.TypeId)
                {
                    menuType.MenuItems.Add(newMenuItem);
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgMenu, newMenuItem);
            addMenuItemWindow.ResetWindow();
        }

        private void btnModifyMenu_Click(object sender, RoutedEventArgs e)
        {
            RS_MenuItemType selectedType = dtgType.SelectedItem as RS_MenuItemType;
            RS_MenuItem selectedMenuItem = dtgMenu.SelectedItem as RS_MenuItem;

            if (selectedType != null && selectedMenuItem != null)
            {
                AddMenuItemWindow modifyMenuItemWindow = new AddMenuItemWindow(selectedType, _units);
                modifyMenuItemWindow.MenuItem = selectedMenuItem;
                modifyMenuItemWindow.InitializeWindow(CrudType.Update);
                modifyMenuItemWindow.AddSuccess += new EventHandler(modifyMenuItemWindow_AddSuccess);

                modifyMenuItemWindow.ShowDialog();
            }
        }

        void modifyMenuItemWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyMenuItemWindow = sender as AddMenuItemWindow;
            RS_MenuItem newMenuItem = modifyMenuItemWindow.MenuItem;

            _menuTypes.Find(delegate(RS_MenuItemType menuType)
            {
                if (menuType.TypeId == newMenuItem.Metadata.TypeId)
                {
                    menuType.MenuItems.Find(delegate(RS_MenuItem menuItem)
                    {
                        if (menuItem.MenuItemId == newMenuItem.MenuItemId)
                        {
                            menuItem = newMenuItem;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgMenu, newMenuItem);
        }

        private void btnDeleteMenu_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dtgType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RS_MenuItemType selectedType = dtgType.SelectedItem as RS_MenuItemType;
            if (selectedType != null)
            {
                ClientUtil.LoadItemsSource(dtgMenu, selectedType.MenuItems);
            }
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid.HasItems)
            {
                dataGrid.SelectedIndex = 0;
            }
        }

        private void dtgLabel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
